<?php
/**
*  Project Name:  NET114.COM
*  @package :  class.main
*  @classID :  Generator.class.php
*  @author  :  邝达峰  
*  @date    : 2014-03-28
* 生成器函数集
*/

class Generator
{

	/**
     * 生成经过一次md5加密过后的唯一的32位id
     * @param string $extra_factor 其它随机因子
     * @access public
     * @return string
     */
	public static function genMd5UniqID($extra_factor = '')
	{
		/**
		 * 以随机数、时间戳、IP地址为因子生成的32位字符串可以保证是随机和唯一的。
		 */
		list($usec, $sec) = explode(" ", microtime());
		$unique_id = md5(uniqid(mt_rand(), true) . $usec . $sec . mt_rand() . getClientIp() . $extra_factor);
		return $unique_id;
	}

	/**
     * 生成2次md5加密过后的字符串
     * @param string $string 原始字符串
     * @return string
     */
	public static function gen2Md5Str($string)
	{
		$md5_string_one = md5($string);
		$md5_string_two = md5($md5_string_one);
		return $md5_string_two ;
	}

	/**
    * 生成随机字符串
    * @param int    $len                随机字符串长度
    * @param bool   $with_digit         是否包含数字
    * @param bool   $with_lower_letter  是否包含小写字母
    * @param bool   $with_upper_letter  是否包含大写字母
    * @param string $with_chars          包含其它字符.如: '.,:!-_'
    * @return string
    */
	public static function genRandomStr($len = 6, $with_digit = true, $with_lower_letter = true, $with_upper_letter = true, $with_chars = '')
	{
		$with_figure = '0123456789';
		$with_lower  = 'abcdefhjmnpqrstuvwxyz';
		$with_upper  = 'ABCDEFGHJKLMNPQRSTUVWYXZ';
		$with_string = '';
		$rand_string = '';

		/* 选择随机生成的字符所包含的字符. */
		if($with_digit == true)
		{
			$with_string .= $with_figure;
		}

		if($with_lower_letter == true)
		{
			$with_string .= $with_lower;
		}

		if($with_upper_letter)
		{
			$with_string .= $with_upper;
		}

		$with_string .= $with_chars;
		$with_length  = strlen($with_string);

		for($i = 0 ; $i < $len ; $i ++)
		{
			/* 随机定位字符所在的位置. */
			mt_srand((double)microtime()*1000000);
			$rand_val = mt_rand(0, $with_length - 1 );
			/* 每次输出一个字符 */
			$rand_string .= substr($with_string, $rand_val, 1) ;
		}

		return $rand_string ;
	}

	/**
	 * 根据操作系统类型，生换行符号
	 * @return string
	 * @access public
	 */
	public static function genNewLine()
	{
		if (substr(php_uname(), 0, 7) == "Windows") {
			return "\r\n" ;
		} else {
			return "\n";
		}
	}

	/**
	*产生一个指定长度的随机字符串
	*@param $len 产生字符串的位数
	*@return str
    **/
	public static function randStr($len,$min = 0)
	{
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz=';
		$charslen = strlen($chars)-1;
		if($len < $min) $len = $min;
		$string='';
		for(;$len>=1;$len--)
		{
			$position=rand(0,$charslen);
			$string.=$chars{$position};
		}
		return $string;
	}

	/**
	*登陆Cookie加密
	*@param $str 要加密的字符串
	*@return str
	**/
	public static function logEnCode($str)
	{
		$str = (string)$str;
		$huyang=new Crypt();
		$chars='AgCaEeGqIf';
		return $huyang->encode(rand(0,9).$chars{$str%5}.self::randStr($str%5,1).~$str.self::randStr($str%7,1).$chars{$str%7});
	}

	/**
	*登陆Cookie解密
	*@param $str 要解密的字符串
	*@return str
    **/
	public static function logDeCode($str)
	{
		$huyang=new Crypt();
		$chars='AgCaEeGqIf';
		try {
			if(empty($str) || is_null($str))
			throw new Exception("读取文件失败！");
			elseif(strlen($str) > 200)
			{
				$str = $huyang->decode($str);
				$i = @strpos($chars, $str{0});
				$i = $i < 2 ? 2:$i;
				$str = substr($str,++$i);
				$i = @strpos($chars, substr($str,-1));
				$i = $i < 5 ? -5:-1*$i;
				$str = substr($str,0,--$i);
				$str = base64_decode($str);
				$i = @strpos($chars, $str{0});
				$i = $i < 3 ? 3:$i;
				$str = substr($str,++$i);
				$str = $huyang->decode($str);
				$i = @strpos($chars, substr($str,-1));
				$i = $i < 4 ? -4:-1*$i;
				$str = substr($str,0,--$i);
				$str = floatval(base64_decode($str));
			}
			else
			{
				$str = $huyang->decode($str);
				$i = strpos($chars, $str{1});
				$i = $i < 1 ? 1:$i;
				$e = strpos($chars, substr($str,-1));
				$e = $e < 1 ? -1:-1*$e;
				$str = substr($str,2+$i,-1+$e);
				$str = is_numeric($str)?~floatval($str):floatval(~$str);
			}
		} catch (Exception $e) { $str = '';}
		return $str;
	}

	/**
	*信息ID加密解密
	**函数名称：	**函数名称：genInfoId($str,$type='en')
	*@param $str 要解密的字符串
	*@return string
	**/
	public static function genInfoId($str,$type='en')
	{
		$chars = 'bdtakiefucwlspxnqymvozhj5r32g9017864g901'; //密钥8位为一组
		$chars_len = strlen($chars);
		if($type == 'en')
		{
			$str = base_convert($str,10,8);
			$str_arr = str_split($str);
			$str = '';
			foreach($str_arr as $k=>$v)
			{
				$str .=	$chars{$k*8%$chars_len+$v};
			}
		}
		else
		{
			$str_arr = str_split($str);
			$str = '';
			foreach($str_arr as $v)
			{
				$str .=	strpos($chars, $v)%8;
			}
			$str = base_convert($str,8,10);
		}
		return $str;
	}

	
}
?>